<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-loading="dialogInfo.btLoading"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-filter
      v-model:query="filterInfo.query"
      :filter-list="filterInfo.list"
    />
    <page-table
      :refresh="tableInfo.refresh"
      ref="selectOrgTable"
      :init-curpage="tableInfo.initCurpage"
      v-model:data="tableInfo.data"
      :api="api"
      :listen-height="false"
      :query="filterInfo.query"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :reserve-selection="true"
      :checked-list="defaultList"
      @handleEvent="handleEvent"
    />
  </page-dialog>
</template>

<script setup name="selectOrg">
import { getOrgPages } from '@/api/base.js'
const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  api: {
    type: Function,
    default: getOrgPages,
  },
  showDialog: {
    type: Boolean,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  defaultList: {
    type: Array,
    default: function () {
      return []
    },
  },
})
// 过滤相关配置
const filterInfo = ref({
  query: {
    sort: 'create_time desc',
    ...props.query,
  },
  list: [
    {
      type: 'input',
      label: '名称',
      value: 'nameLike',
      event: search,
    },
    { type: 'button', label: '搜索', event: search },
  ],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  fieldList: [
    { label: '机构名称', value: 'name' },
    { label: '机构编码', value: 'code' },
  ],
})
// 弹窗相关
const dialogInfo = ref({
  title: '选择机构',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: true,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref('')
watch(
  () => tableInfo.value.data,
  (val) => {
    if (val.length) {
      toggleSelection(val)
    }
  }
)
const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

onMounted(() => {
  proxy.$nextTick(() => {
    getList()
  })
})

function toggleSelection(val) {
  let rows =
    val.filter((row) => props.defaultList.includes(row.id) || row.disabled) ||
    []
  if (rows.length) {
    proxy.$nextTick(() => {
      rows.forEach((row) =>
        proxy.$refs['selectOrgTable'].toggleRowSelection(row, true)
      )
    })
  }
}
// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function save() {
  // emits('input', data)
  if (selectRow.value) {
    handleClose()
    emits('callback', selectRow.value)
  } else {
    proxy.$message({
      type: 'warning',
      duration: 3500,
      showClose: true,
      message: '请选择供应商',
    })
  }
}
function handleEvent(event, data) {
  switch (event) {
    case 'tableClickRow':
      selectRow.value = data
      dialogInfo.value.btList[1].disabled = !selectRow.value
      break
    case 'tableCheck':
      if (data.length) {
        selectRow.value = data
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
</script>

<style scoped lang="scss">
:deep(.el-dialog__body) {
  height: 355px;
  display: flex;
  flex-direction: column;
}
</style>
